package org.example.managere.dto.feibiao;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.DecimalMax;
import java.math.BigDecimal;
import java.io.Serializable;
import java.util.Date;

/**
 * 电机实时运行监测数据DTO - 电机向量知识库
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AmKnowledgeOperationDto implements Serializable {
    private Integer id; // 运行记录唯一标识
    
    @NotEmpty(message = "电机ID不能为空")
    @Size(max = 10, message = "电机ID长度不能超过10")
    private String motorId; // 关联电机表motor_id
    
    @NotNull(message = "采集时间不能为空")
    private Date collectionTime; // 数据采集时间(YYYY-MM-DD HH:MM:SS)
    
    @NotNull(message = "实时电压不能为空")
    @DecimalMin(value = "0", message = "电压不能小于0V")
    @DecimalMax(value = "1000", message = "电压不能大于1000V")
    private BigDecimal actualVoltage; // 实时电压(V)
    
    @NotNull(message = "实时电流不能为空")
    @DecimalMin(value = "0", message = "电流不能小于0A")
    @DecimalMax(value = "500", message = "电流不能大于500A")
    private BigDecimal actualCurrent; // 实时电流(A)
    
    @NotNull(message = "工作温度不能为空")
    @DecimalMin(value = "-40", message = "温度不能低于-40℃")
    @DecimalMax(value = "200", message = "温度不能高于200℃")
    private BigDecimal operatingTemperature; // 工作温度(℃)
    
    @NotNull(message = "转速不能为空")
    @DecimalMin(value = "0", message = "转速不能小于0rpm")
    @DecimalMax(value = "20000", message = "转速不能大于20000rpm")
    private BigDecimal rotationSpeed; // 转速(rpm)
    
    private Date createdAt;
    private Date updatedAt;
    
    // 添加额外字段以便前端显示
    private String motorType;
    private String manufacturer;
    
    // 计算字段 - 负载百分比（根据电流额定值计算）
    private BigDecimal ratedCurrent; // 额定电流值，需从电机基础信息获取
    
    public BigDecimal getLoadPercentage() {
        if (actualCurrent == null || ratedCurrent == null || ratedCurrent.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return actualCurrent.multiply(new BigDecimal("100")).divide(ratedCurrent, 2, BigDecimal.ROUND_HALF_UP);
    }
    
    // 判断是否超出安全温度
    public boolean isTemperatureWarning() {
        if (operatingTemperature == null) {
            return false;
        }
        return operatingTemperature.compareTo(new BigDecimal("85")) > 0;
    }
    
    // 判断是否超出安全电压范围
    public boolean isVoltageWarning() {
        if (actualVoltage == null) {
            return false;
        }
        return actualVoltage.compareTo(new BigDecimal("250")) > 0;
    }
} 